﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using RFIDStockManagement.BLL;
using RFIDStockManagement.Common.Public.Enums;
using System.Threading;

namespace RFIDStockManagement.Client
{
    public partial class SupplierList : XtraUserControl
    {
        public SupplierList()
        {
            InitializeComponent();
        }

        private void SupplierList_Load(object sender, EventArgs e)
        {
            FormSizeChange();
            BindGridData();
        }
        private void SupplierList_SizeChanged(object sender, EventArgs e)
        {
            FormSizeChange();
        }

        private void FormSizeChange()
        {
            gridData.Height = this.Height - (pnlSearch.Location.Y + pnlSearch.Height) + 3;
        }

        private void BindGridData()
        {
            gridView.ShowLoadingPanel();
            ThreadPool.QueueUserWorkItem(new WaitCallback(delegate(object state)
                {
                    try
                    {
                        Dictionary<string, object> filter = new Dictionary<string, object>();
                        if (txtName.Text.Trim().Length > 0)
                        {
                            filter.Add("FName", txtName.Text.Trim());
                        }
                        DataTable dt = null;
                        if (filter.Count > 0)
                        {
                            dt = BasicInfoManager.Search(filter, EnumBasicInfo.Supplier);
                        }
                        else
                        {
                            dt = BasicInfoManager.GetList(filter, EnumBasicInfo.Supplier);
                        }
                        BindGridDataDelegate delegateMothod = new BindGridDataDelegate(BindGridDataHandler);
                        this.Invoke(delegateMothod, dt);
                    }
                    finally
                    {
                        gridView.HideLoadingPanel();
                    }
                }));
        }
        private delegate void BindGridDataDelegate(DataTable dt);
        private void BindGridDataHandler(DataTable dt)
        {
            gridData.DataSource = dt;
        }

        private void barBtnEdit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                int[] selectRows = gridView.GetSelectedRows();
                if (selectRows != null && selectRows.Length > 0)
                {
                    DataRowView rowView = gridView.GetRow(selectRows[0]) as DataRowView;
                    if (rowView != null)
                    {
                        long id = long.Parse(rowView.Row["FID"].ToString());
                        SupplierEdit editForm = new SupplierEdit(id);
                        editForm.ShowDialog();
                        BindGridData();
                    }
                }
            }
            catch (Exception ex)
            {
                AlertForm.ShowError(ex.Message);
            }
        }

        private void barBtnRefresh_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            txtName.Text = string.Empty;
            BindGridData();
        }

        private void barBtnNew_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                SupplierEdit editForm = new SupplierEdit(0);
                editForm.ShowDialog();
                barBtnRefresh_ItemClick(null, null);
            }
            catch (Exception ex)
            {
                AlertForm.ShowError(ex.Message);
            }
        }

        private void barBtnDelete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                int[] selectRows = gridView.GetSelectedRows();
                if (selectRows == null || selectRows.Length == 0)
                {
                    AlertForm.ShowMessage("至少要选择一行才能删除");
                    return;
                }
                if (AlertForm.Confirm("确定删除所有选择的数据？"))
                {
                    List<string> lstId = new List<string>();
                    foreach (int iRow in selectRows)
                    {
                        DataRowView rowView = gridView.GetRow(iRow) as DataRowView;
                        if (rowView != null)
                        {
                            lstId.Add(rowView.Row["FID"].ToString());
                        }
                    }
                    if (lstId.Count > 0)
                    {
                        List<string> result = BasicInfoManager.Delete(lstId, EnumBasicInfo.Supplier);
                        if (result.Count == 1)
                        {
                            AlertForm.ShowMessage(result[0]);
                        }
                        else
                        {
                            AlertForm.ShowMessage(result);
                        }
                        BindGridData();
                    }
                }
            }
            catch (Exception ex)
            {
                AlertForm.ShowError(ex.Message);
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            SearchData();
        }
        private void SearchData()
        {
            if (txtName.Text.Trim().Length == 0)
            {
                AlertForm.ShowMessage("请输入供应商名称再搜索");
                return;
            }
            BindGridData();
            //gridView.ShowLoadingPanel();
            //ThreadPool.QueueUserWorkItem(new WaitCallback(delegate(object state)
            //{
            //    try
            //    {
            //        Dictionary<string, string> filter = new Dictionary<string, string>();
            //        filter.Add("FName", txtName.Text.Trim());
            //        DataTable dt = BasicInfoManager.Search(filter, EnumBasicInfo.Supplier);
            //        BindGridDataDelegate delegateMothod = new BindGridDataDelegate(BindGridDataHandler);
            //        this.Invoke(delegateMothod, dt);
            //    }
            //    finally
            //    {
            //        gridView.HideLoadingPanel();
            //    }
            //}));
        }

        private void txtName_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                SearchData();
            }
        }
    }
}
